/**
 * Project Name:springBoot-controller
 * File Name:MongoDBController.java
 * Package Name:com.libra.controller
 * Date:2018年8月10日下午2:50:28
 * Copyright (c) 2018, wangzhch All Rights Reserved.
 *
*/

package com.libra.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.libra.model.mongoDb.Users;
import com.libra.service.MongoDBUserTestService;

/**
 * ClassName:MongoDBController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2018年8月10日 下午2:50:28 <br/>
 * @author   wangzhch
 * @version  
 * @since    JDK 1.8
 * @see 	 
 */
@RestController
@RequestMapping("mongoDB")
public class MongoDBController {
	private Logger log = LoggerFactory.getLogger(MongoDBController.class);
	@Autowired
	private MongoDBUserTestService mongoDBUserTestService;
	
	/**
	 * 
	 * saveUser:(mongodb新增). <br/>
	 * @author wangzhch
	 * @param users
	 * @since JDK 1.8
	 */
	@RequestMapping(value="/saveUser",method = RequestMethod.POST)
	public void saveUser(@RequestBody Users users) {
		log.info("添加      =>   MongoDBController saveUser request:{}",JSON.toJSONString(users));
		mongoDBUserTestService.saveUser(users);
	}
	/**
	 * 
	 * findUserByName:(查找单个). <br/>
	 * @author wangzhch
	 * @param name
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value="/findUserByPrimaryKey",method = RequestMethod.POST)
	public Users findUserByPrimaryKey(@RequestBody Users users) {
		log.info("查询单个      =>   MongoDBController findUserByPrimaryKey request:{}",JSON.toJSONString(users));
		users= mongoDBUserTestService.findUserByPrimaryKey(users.getId());
		log.info("查询单个      =>   MongoDBController findUserByPrimaryKey response:{}",JSON.toJSONString(users));
		return users;
	}
	/**
	 * 
	 * removeUser:(删除id). <br/>
	 * TODO(这里传的是主键删除的是单个,否则可能会删除多个).<br/>
	 * 
	 * @author wangzhch
	 * @param name
	 * @since JDK 1.8
	 */
	@RequestMapping(value="/removeUserByPrimaryKey",method = RequestMethod.POST)
	public Long removeUserByPrimaryKey(@RequestBody Users users) {
		log.info("删除单个      =>   MongoDBController removeUserByPrimaryKey request:{}",JSON.toJSONString(users));
		Long num = mongoDBUserTestService.removeUser(users.getId());
		log.info("删除单个{}",num==1?"成功":"失败");
		return num;
	}
	/**
	 * 
	 * updateUser:(修改). <br/>
	 *
	 * @author wangzhch
	 * @param name 条件
	 * @param key  具体字段
	 * @param value 具体值
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/updateUserByPrimaryKey",method = RequestMethod.POST)
	public Long updateUserByPrimaryKey(@RequestBody Users users) {
		log.info("修改      =>   MongoDBController updateUser request:{}",JSON.toJSONString(users));
		Long num = mongoDBUserTestService.updateUserByPrimaryKey(users);
		log.info("修改单个{}",num==1?"成功":"失败");
		return num;
	}
	
	/**
	 * 
	 * listUser:(无条件查询所有数据). <br/>
	 * 
	 * @author wangzhch
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/listUser" , method = RequestMethod.POST)
	public List<Users> listUser() {
		log.info("无条件查询集合      =>");
		List<Users> list = mongoDBUserTestService.listUser();
		log.info("无条件查询集合size()      =>   MongoDBController listUser response:{}",list.size());
		log.info("无条件查询集合Object()      =>   MongoDBController listUser response:{}",JSON.toJSONString(list));
		return list;
	}
	/**
	 * 
	 * listUserByName:(条件查询集合). <br/>
	 *
	 * @author wangzhch
	 * @param name
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/listUserByName", method = RequestMethod.POST)
	public List<Users> listUserByName(@RequestBody Users users) {
		log.info("条件查询集合      =>   MongoDBController listUserByName request:{}",JSON.toJSONString(users));
		List<Users> list = mongoDBUserTestService.listUser(users.getName());
		log.info("条件查询集合      =>   MongoDBController listUser response:{}",list.size());
		log.info("无条件查询集合Object()      =>   MongoDBController listUser response:{}",JSON.toJSONString(list));
		return list;
	}
	public static void main(String[] args) {
		System.out.println("模拟冲突");
	}
}

